import 'dart:ui';

import 'package:intl/intl.dart';
import 'package:my_meeting/entity/meeting_data.dart';
import 'package:my_meeting/main.dart';
import 'package:my_meeting/utils/log_utils.dart';

import '../generated/l10n.dart';

class Utils{
  static String weekdayName(String? date){
    if(date == null){
      return '';
    }
    LogD("weekdayName-$date");
    DateFormat format = DateFormat("yyyy.MM.dd"); // 根据字符串格式设置
    DateTime dateTime = format.parse(date);
    String weekdayName;
    switch (dateTime.weekday) {
      case 1:
        weekdayName =  S.current.monday;
        break;
      case 2:
        weekdayName = S.current.tuesday;
        break;
      case 3:
        weekdayName = S.current.wednesday;
        break;
      case 4:
        weekdayName = S.current.thursday;
        break;
      case 5:
        weekdayName = S.current.friday;
        break;
      case 6:
        weekdayName = S.current.saturday;
        break;
      case 7:
        weekdayName = S.current.sunday;
        break;
      default:
        weekdayName = "未知";
    }
    return weekdayName;
  }

  static String getMeetingTime(String? str){
    if(!stringIsEmpty(str)){
      List<String> arr = str!.split(' ');
      if(arr.last.length > 5){
        return arr.last.substring(0,5);
      }
      return arr.last;
    }
    return '-';
  }

  static String getMeetingDate(String? str){
    if(!stringIsEmpty(str)){
      List<String> arr = str!.split(' ');
      DateFormat format = DateFormat('yyyy.MM.dd');
      DateTime date = format.parse(arr.first);
      DateFormat format1 = DateFormat('dd.MM.yyyy');
      return format1.format(date);
    }
    return '-';
  }

  static bool stringIsEmpty(String? str) {
    if (str != null && str.isNotEmpty) {
      return false;
    }
    return true;
  }

  static int timeDifference(String start,String end){
    DateFormat format = DateFormat("yyyy.MM.dd HH:mm");
    DateTime startTime = format.parse(start); // 解析起始时间
    DateTime endTime = format.parse(end); // 解析结束时间

    // 计算时间差
    Duration difference = endTime.difference(startTime);

    // 获取小时差
    int hoursDifference = difference.inHours;
    // int minutesDifference = difference.inMinutes.remainder(60);
    return hoursDifference;
  }

  //0待开始 1进行中 2已结束
  static String meetingState(MeetingData data){
    if(data.meetingState != null && data.meetingState == 1){
      return S.current.hadCancel;
    }
    String start = data.meetingBeginTime!;
    String end = data.meetingEndTime!;
    DateFormat format = DateFormat("yyyy.MM.dd HH:mm");
    DateTime startTime = format.parse(start); // 解析起始时间
    DateTime endTime = format.parse(end); // 解析结束时间

    // 获取当前时间
    DateTime currentTime = DateTime.now();

    String state = '';
    if(currentTime.isAfter(endTime)){
      state = S.current.hadEnd;
    }else if(currentTime.isAfter(startTime) && currentTime.isBefore(endTime)){
      state = S.current.starting;
    }else {
      state = S.current.willStart;
    }
    return state;
  }

  static Color stateColor(String state){
    Color color = const Color(0xFF1877FF);
    if (state == S.current.hadEnd){
      color = const Color(0xFF333333).withOpacity(0.4);
    }else if(state == S.current.hadCancel){
      color = const Color(0xFF333333).withOpacity(0.4);
    } else if(state == S.current.starting){
      color = const Color(0xFF1877FF);
    } else if(state == S.current.willStart){
      color = const Color(0xFFFC6C54);
    }
    return color;
  }


}